﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using HT.Data.Util;
namespace HT.Data
{
    public class Cookie
    {
        public Cookie()
        {
        }
        #region Binary Serialization
        /// <summary>
        /// Method to get Binary Serialized String
        /// </summary>
        /// <param name="serializableObject"></param>
        /// <returns></returns>
        public string GetBinarySerializedString(object serializableObject)
        {
            return HexEncoding.ToString(Util.Serialization.BinarySerialize(serializableObject));
        }
        #endregion

        #region Save Cookie

        public HttpCookie SaveCookie(string cookieName, string value)
        {
            HttpCookie ck = new HttpCookie(cookieName, value);
            HttpContext.Current.Response.Cookies.Add(ck);
            return ck;
        }
        public HttpCookie SaveCookie(string cookieName, object serializableObject)
        {
            return SaveCookie(cookieName, GetBinarySerializedString(serializableObject));
        }

        /// <summary>
        /// Removes a cookie from Request/Response Object
        /// </summary>
        /// <param name="cookieName"></param>
        public void RemoveCookie(string cookieName)
        {
            HttpContext.Current.Request.Cookies.Remove(cookieName);
            HttpContext.Current.Response.Cookies.Remove(cookieName);
        }
        #endregion



        #region Get Cookie
        /// <summary>
        /// Get HTTPCookie Object
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns></returns>
        public HttpCookie GetCookie(string cookieName)
        {
            return HttpContext.Current.Request.Cookies.Get(cookieName);
        }

        /// <summary>
        /// Gets the Value of Cookie from the HTTPCookie object
        /// </summary>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public string GetCookieValue(HttpCookie cookie)
        {
            return cookie == null ? null : cookie.Value;
        }

        /// <summary>
        /// Gets the Value of Cookie for the given cookieName
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns></returns>
        public string GetCookieValue(string cookieName)
        {
            HttpCookie ck = GetCookie(cookieName);
            return ck == null ? null : GetCookieValue(ck);
        }

        /// <summary>
        /// Gets the DeSerialized Object from Cookie for the given cookieName
        /// </summary>
        /// <param name="name"></param>
        /// <param name="isBinary"></param>
        /// <returns></returns>
        public object GetCookieObject(string name)
        {
            string val = GetCookieValue(name);

            return string.IsNullOrEmpty(val) ? null : DeSerializeCookieValue(val);
        }

        #endregion

        #region DeSerialize
        public object DeSerializeCookieValue(string binaryStr)
        {
            int x = 0;
            return Util.Serialization.BinaryDeSerialize(HexEncoding.GetBytes(binaryStr, out x));
        }
        #endregion

    }
}

